package nosdk;

import org.apache.dubbo.common.beanutil.JavaBeanDescriptor;
import org.apache.dubbo.common.beanutil.JavaBeanSerializeUtil;
import org.apache.dubbo.common.json.JSON;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (c)  by www.leya920.com
 * All right reserved.
 * Create Date: 2020-11-21 18:19
 * Create Author: xuweiwei
 * File Name: ApiGenericConsumerForTrue.java
 * Last version:  1.0
 * Function: //TODO
 * Last Update Date: 2020-11-21 18:19
 * Last Update Log:
 * Comment: //TODO
 **/
public class ApiGenericConsumerForBean {

    public static void main(String[] args) throws IOException {


        // 1 GenericService
        ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<>();

        // 2 设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));

        // 3 设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));

        // 4 设置服务接口  超时时间
        referenceConfig.setInterface("api.GreetingService");
        // 5 设置泛化引用为true
        referenceConfig.setGeneric("bean");
        // 6 超时时间
        referenceConfig.setTimeout(5000);

        // 7 设置服务分子与版本
        referenceConfig.setGroup("dubbo");
        referenceConfig.setVersion("1.0.0");

        // 8 引用服务  用GenericService 代替所有的接口引用
        GenericService greetingService = referenceConfig.get();
        
        // 9 泛型调用 参数使用javabean进行序列化
        JavaBeanDescriptor param = JavaBeanSerializeUtil.serialize("world bean");

        Object result = greetingService.$invoke("sayHello",new String[]{"java.lang.String"},new Object[]{param});


        // 结果反序列化
        result = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor)result);
        System.out.println(JSON.json(result));

    }
}
